﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using Finance.IBll;

using Finance.Entity;
using Finance.Bll;

namespace Finance.UI.Web
{
    public class FinanceMembershipProvider:System.Web.Security.MembershipProvider
    {
        private IUserBll userBll = new UserBll();
        public override string ApplicationName
        {
            get; set; }
        public string EncryptPassword(string password)
        {
            MD5 md5=new MD5CryptoServiceProvider();
            password = password ?? "";
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(password))).Replace("-", "").ToLower();
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var oldPasswordEnc =EncryptPassword(oldPassword);
              var newPasswordEnc =EncryptPassword(newPassword);
            return userBll.ChangePassword(username, oldPasswordEnc, newPasswordEnc);
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        //public override System.Web.Security.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out System.Web.Security.MembershipCreateStatus status)
        //{
        //    userBll.CreateUser(username, EncryptPassword(password), email, passwordQuestion, passwordAnswer, isApproved, providerUserKey);
        //    status=MembershipCreateStatus.Success;
        //    return this.GetUser(providerUserKey, false);
        //    //return new MembershipUser(this.ApplicationName, username,providerUserKey,email,passwordQuestion,null,isApproved,false,DateTime.Now,DateTime.Now,DateTime.Now,DateTime.Now,DateTime.Now );
        //}

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return userBll.DeleteUser(username, deleteAllRelatedData);
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Web.Security.MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUser GetUser(string username, bool userIsOnline)
        {
            UserEntity user = userBll.GetUser(username, userIsOnline);
            return MembershipUserFrom(user);
        }

        public override System.Web.Security.MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            UserEntity user = userBll.GetUser(providerUserKey, userIsOnline);
            return MembershipUserFrom(user);
        }

        private MembershipUser MembershipUserFrom(UserEntity userEnity)
        {
            if (userEnity != null)
            {
                return new MembershipUser(this.Name,userEnity.UserName,userEnity.ProviderUserKey,userEnity.Email,userEnity.PasswordQuestion,null,userEnity.IsApproved==1,false,userEnity.CreateTime.Value,userEnity.LastLoginTime.Value,DateTime.MinValue,DateTime.MinValue,DateTime.MinValue);
            }
            return null;
        }

        public override string GetUserNameByEmail(string email)
        {
            return userBll.GetUserNameByEmail(email);
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 6; }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Web.Security.MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Hashed; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return ".{6,}"; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get {return  false; }
        }

        public override string ResetPassword(string username, string answer)
        {
            string password = new Random().Next(100000, 999999999).ToString();
            if (userBll.ResetPassword(username, answer, EncryptPassword(password)))
            {
                return password;
            }
            return null;
        }

        public override bool UnlockUser(string userName)
        {
            return userBll.UnlockUser(userName);
        }

        //public override void UpdateUser(System.Web.Security.MembershipUser user)
        //{
        //    UserEntity userEntity = new UserEntity();
        //    userEntity.CreateTime = user.CreationDate;
        //    userEntity.Email = user.Email;
        //    userEntity.IsApproved = user.IsApproved ? 1 : 0;
        //    userEntity.LastLoginTime = user.LastLoginDate;
        //    userEntity.PasswordQuestion = user.PasswordQuestion;
        //    userEntity.ProviderUserKey = user.ProviderUserKey as string;

        //    userBll.UpdateUser(userEntity);
        //}

        public override bool ValidateUser(string username, string password)
        {
            var passwordEnc = EncryptPassword(password);
            return userBll.ValidateUser(username, passwordEnc);
        }
    }
}